import tkinter as tk
from tkinter import messagebox

# 哈希函数
def hash_function(str_or_num, num):
    # 如果输入是数字，先转换为字符串
    if isinstance(str_or_num, int):
        str_or_num = str(str_or_num)
    table = {}
    code = 0  # 散列数值
    salt = 37  # 盐（固定加密码）
    for char in str_or_num:
        code = salt * code + ord(char)  # 返回字符的编码
    # 取余
    mod = code % num
    # 追加到散列表中
    if isinstance(table.get(mod), list):
        if str_or_num not in table[mod]:
            table[mod].append(str_or_num)
    else:
        table[mod] = [str_or_num]
    return table

# 创建主窗口
root = tk.Tk()
root.title("哈希函数")

# 创建按钮来执行哈希函数
def run_hash_function():
    table = hash_function(123, 7)
    hash_function('abc', 7)
    hash_function('你好', 7)
    hash_function(123, 7)
    messagebox.showinfo("哈希表结果", f"哈希表: {table}, table[3]: {table.get(3)}")

hash_button = tk.Button(root, text="运行哈希函数", command=run_hash_function)
hash_button.pack(pady=10)

# 运行主循环
root.mainloop()